package com.ljs.liojbackendserviceclient.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;

import com.ljs.libackendmodel.model.dto.user.UserQueryRequest;
import com.ljs.libackendmodel.model.entity.User;
import com.ljs.libackendmodel.model.enums.UserRoleEnum;
import com.ljs.libackendmodel.model.vo.LoginUserVO;
import com.ljs.libackendmodel.model.vo.UserVO;
import com.ljs.liojbackendcommon.common.ErrorCode;
import com.ljs.liojbackendcommon.exception.BusinessException;
import org.springframework.beans.BeanUtils;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;


import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;

import static com.ljs.liojbackendcommon.constant.UserConstant.USER_LOGIN_STATE;



@FeignClient(name = "lioj-backend-user-service",path = "/api/user/inner")
public interface UserFeignClient {

    /**
     * 根据id、获取用户
     * @param userId
     * @return
     */
    @GetMapping("/get/id")
    User getById(@RequestParam("userId") long userId);

    /**
     * 根据id获取用户列表
     * @param idList
     * @return
     */
    @GetMapping("/get/ids")
    List<User> listByIds(@RequestParam("idList") Collection<Long> idList);


    /**
     * 获取当前登录用户
     *
     * @param request
     * @return
     */
  default   User getLoginUser(HttpServletRequest request){
      // 先判断是否已登录
      Object userObj = request.getSession().getAttribute(USER_LOGIN_STATE);
      User currentUser = (User) userObj;
      if (currentUser == null || currentUser.getId() == null) {
          throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
      }

      return currentUser;
  }



    /**
     * 是否为管理员
     *
     * @param user
     * @return
     */
  default   boolean isAdmin(User user){
      return user != null && UserRoleEnum.ADMIN.getValue().equals(user.getUserRole());
  }





    /**
     * 获取脱敏的用户信息
     *
     * @param user
     * @return
     */
   default UserVO getUserVO(User user){
       if (user == null) {
           return null;
       }
       UserVO userVO = new UserVO();
       BeanUtils.copyProperties(user, userVO);
       return userVO;


   }




}
